const path = require("path")
const webpack = require("webpack")
const ExtractTextPlugin = require('extract-text-webpack-plugin')

module.exports = {
    entry: {
        index: './scripts/index.js',
    },
    output: {
        path: path.join(__dirname, 'dist'),
        filename: "scripts/[name].pack.js",
        sourceMapFilename: "scripts/[name].pack.js.map"
    },
    cache: false,
    debug: false,
    devtool: 'source-map',
    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: /(node_modules|bower_components)/,
                loader: ["babel"], // 'babel-loader' is also a legal name to reference
                query: {
                    presets: ["react", "es2015", "stage-0"]
                }
            }
            , {
                test: /\.css$/,
                // ExtractTextPlugin.extract([notExtractLoader], loader, [options]) https://github.com/webpack/extract-text-webpack-plugin#api
                loader: ExtractTextPlugin.extract('style-loader', 'css-loader?modules&importLoaders=1&localIdentName=[hash:base64:6]!postcss-loader')
            }
            , {
                test: /\.(jpe?g|png|gif|svg|webp)$/i,
                loaders: [
                    'file?hash=sha512&digest=hex&name=./images/[hash].[ext]',
                    // https://github.com/tcoopman/image-webpack-loader
                    'image-webpack'
                ]
            }
        ]
    },

    postcss: [
        require('postcss-cssnext'),
        require('postcss-import'),
        require('postcss-mixins'),
        require('postcss-inline-svg'),
        require('postcss-sorting'),
        require('cssnano'),
        require('stylelint')
    ],

    imageWebpackLoader: {
        pngquant:{
            quality: "65-90",
            speed: 4
        },
        svgo:{
            plugins: [
                {removeViewBox: false},
                {removeEmptyAttrs: false}
            ]
        }
    },

    node: { // fix issus from joi https://github.com/hapijs/joi/issues/665#issuecomment-113713020
          net: 'empty',
          tls: 'empty',
          dns: 'empty'
    },
    
    plugins: [
        new ExtractTextPlugin('style.css', { allChunks: true }),
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production')
            }
        })
        , new webpack.optimize.UglifyJsPlugin() // https://webpack.github.io/docs/optimization.html#minimize
        , new webpack.NoErrorsPlugin()
    ]
}
